/**
* Author: 	XU Songgang 
* Email:	sxu@ust.hk
*
*/
#ifndef _PPM_TEST_
#define _PPM_TEST_

#include <fstream>
#include <string>
#include <cstdlib>
#include "global.h"
using namespace std;
#include "assert.hpp"
#include "../PPM.h"

bool compare_images(const char * output, const char * solution)
{
	ifstream output_if(output);
	ifstream solution_if(solution);
	string s_line, o_line;
	int s_row, s_col, o_row, o_col;

	getline(solution_if, s_line);
	if (! getline(output_if, o_line)) return false;

        getline(solution_if, s_line);
        if (! getline(output_if, o_line)) return false;

	solution_if>>s_row>>s_col;
	if (! (output_if>>o_row)) return false;
	if (! (s_row == o_row))	return false;

	if (! (output_if>>o_col)) return false;
	if (! (s_col == o_col)) return false;


        getline(solution_if, s_line);
        if (! getline(output_if, o_line)) return false;

        getline(solution_if, s_line);
        if (! getline(output_if, o_line)) return false;

	for (int i = 0; i < s_row * s_col * 3; ++i)
	{
		int s_value, o_value;
		solution_if>>s_value;
		if (!(output_if>>o_value)) return false;
		if (!(s_value == o_value )) return false;
	}
	return true;
}
void PPM_load_save()
{
	PPM image;
	image.load("./testsuite/Long_comment.ppm");
	image.save("Long_comment_saved.ppm");
	ASSERT(true == compare_images("./Long_comment_saved.ppm", "./testsuite/Long_comment_saved.ppm"));

	image.load("./testsuite/Snail_original.ppm");	
	image.save("Snail_saved.ppm");
	ASSERT(true == compare_images("./Snail_saved.ppm", "./testsuite/Snail_saved.ppm"));
}

void PPM_invert()
{
	PPM image;
	image.load("./testsuite/Snail_original.ppm");
	
	image.invert();
	image.save("Snail_inverted.ppm");
	ASSERT(true == compare_images("./Snail_inverted.ppm", "./testsuite/Snail_inverted.ppm"));
}
void PPM_enlarge_snail_2()
{
	PPM image;
	image.load("./testsuite/Snail_original.ppm");

	image.enlarge(2);
	image.save("Snail_enlarged.ppm");
	ASSERT(true == compare_images("./Snail_enlarged.ppm", "./testsuite/Snail_enlarged.ppm"));
}
void PPM_enlarge_logo_2()
{
	PPM image;
	image.load("./testsuite/hkust_logo.ppm");
	
	image.enlarge(2);
	image.save("hkust_logo_enlarged_2.ppm");
	ASSERT(true == compare_images("./hkust_logo_enlarged_2.ppm", "./testsuite/hkust_logo_enlarged_2.ppm"));
}
void PPM_enlarge_boxes_3()
{
	PPM image;
	image.load("./testsuite/boxes.ppm");

	image.enlarge(3);
	image.save("boxes_enlarged_3.ppm");
	ASSERT(true == compare_images("./boxes_enlarged_3.ppm", "./testsuite/boxes_enlarged_3.ppm"));
}
void PPM_test_suite()
{
        TEST_SUITE_BEGIN("PPM_test_suite");
/*
        TEST(PPM_load_save());                  //5
        TEST(PPM_invert());                     //5
        TEST(PPM_enlarge_snail_2());            //5
        TEST(PPM_enlarge_logo_2());             //5
        TEST(PPM_enlarge_boxes_3());            //5
*/
        TEST_SUITE_END();
}
#endif
